<?php
	
	$characters = '0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz0123456789abcdefghijk0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz0123456789abcdefghijk';
	$id = '';
	for ($p = 0; $p < 50; $p++) {
		$id .= $characters[mt_rand(0, strlen($characters))];
	}

	include("../Model/Connection.php");

    $destination_path = "../Apps/";

    $result = 0;
    $filename = $_FILES['apk']['name'];
    $target_path = $destination_path . basename( $_FILES['apk']['name']);

    if($_FILES['apk']['size'] < 0){
   		echo "Error o1";
    }
   else if($_POST['game']=='gameapp' && $_POST['app'] == 'appapp'){
   		echo "Error o2";
   }
   else if($_POST['game'] !='gameapp' && $_POST['app'] != 'appapp'){
   		echo "Error 03";
   }
   else if(empty($_POST['name']) || empty($_POST['message'])){
   		echo "Error o4";
   }
   		
   else{
	   
	   		if(@move_uploaded_file($_FILES['apk']['tmp_name'], $target_path)) {
	   			
	   			$appname = $_POST['name'];
	   			$des     = $_POST['message'];
	   			$cate = "updated";
	   			$video = "nothing";
	   			
				if(isset($_POST['app']) && $_POST['game'] == 'gameapp'){
				   					$cate = $_POST['app'];
				}
				else{
				  	$cate = $_POST['game'];
				}
				if(isset($_POST['video'])){
					$video = $_POST['video'];
				}
				
				/*Them avatar vao database*/
				
					$tmpName  = $_FILES['avatar']['tmp_name'];
				
					// Read the file
					$fp      = fopen($tmpName, 'r');
					$data = fread($fp, filesize($tmpName));
					$data = addslashes($data);
				
				/*--------------------------*/
				$sql = "INSERT INTO UNG_DUNG(ID, TEN_UNG_DUNG, MO_TA, LIEN_KET_VIDEO, TRONG_DANH_MUC, apk, avatar) VALUES ('$id', '$appname', '$des', '$video','$cate', '$filename', '$data')";
				mysql_query($sql) or die("Loi ne -->".mysql_error());
				$result = 1;
				   		
	   		   
	   		}
	   		header("Location: ../Views/upload_app01.php?id=$id");
	}
?>



